
//
//  collectMovieViewController.m
//  电影豆瓣--完整版
//
//  Created by dllo on 16/1/9.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "collectMovieViewController.h"
#import "DataBaseHandle.h"
#import "MyModel.h"
#import "movieModel.h"
#import "MovieMessageViewController.h"
@interface collectMovieViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *modelArr;

@property (nonatomic, retain) NSMutableArray *arrMovie; /** < 从数据库取出的已被收藏的电影的数组*/

@end

@implementation collectMovieViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellMovie"];
    
    [self dataHandle];
    
    self.arrMovie = [NSMutableArray array];
    self.arrMovie =  [[DataBaseHandle sharedDataBaseHandle] selectAllFromtable:@"Movie"];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _arrMovie.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellMovie"];
    MyModel *model = _arrMovie[indexPath.row];
    cell.textLabel.text = model.title;
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MyModel *model1 = _arrMovie[indexPath.row];
    // 从原来的活动大数组中来找我点击的这个电影
    for (movieModel *model in _modelArr) {
        // 如果找到，则跳转到活动详情页面,并传值
        if ([model.title isEqualToString:model1.title]) {
            MovieMessageViewController *movieVC = [[MovieMessageViewController alloc] init];
            movieVC.model = model;
            [self.navigationController pushViewController:movieVC animated:YES];
            break;
        }
        
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}



// 数据处理
- (void)dataHandle {
    self.modelArr = [NSMutableArray array];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"plist"];
    NSArray *tempArr = [NSArray arrayWithContentsOfFile:path];
    for (NSDictionary *dic in tempArr) {
        movieModel *model = [movieModel movieModel];
        [model setValuesForKeysWithDictionary:dic];
        [_modelArr addObject:model];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
